/****************************************************************************/
/***                                                                      ***/
/***   (C) 2012-2014 Dierk Ohlerich et al., all rights reserved.          ***/
/***                                                                      ***/
/***   Released under BSD 2 clause license, see LICENSE.TXT               ***/
/***                                                                      ***/
/****************************************************************************/

mtrl sDistanceFieldShaderXX
{
    struct ia2vs
    {
        float3 pos : POSITION;
        float4 color : COLOR;
        float2 uv0 : TEXCOORD0;
    };
    struct vs2ps
    {
        float4 color : COLOR;
        float2 uv0 : TEXCOORD0;
    };
	
    cbuffer cbvs : vs(0) : register(0)
    {
        row_major float4x4 MS2SS;
    };
  
    cbuffer cbps : ps(0) : register(0)
    {
        float4 Para;         // sub, mul, 0, 0
    };
  
    // DX9

    vs hlsl3
    {
        void main(in ia2vs i,out vs2ps o,out float4 tpos : POSITION)
        {
            tpos = mul(MS2SS,float4(i.pos,1));
            o.color = i.color;
            o.uv0 = i.uv0;
        }
    };
    ps hlsl3
    {
        SamplerState sam0 : register(s0);
  
        void main(in vs2ps i,out float4 r : COLOR)
        {
            float4 tex = tex2D(sam0,i.uv0);
        
            float f = saturate((tex.r - Para.x) * Para.y);           
            r = i.color*f;      // premultiplied alpha!
        }
    };

    // DX11  
  
    vs hlsl5
    {
        void main(in ia2vs i,out vs2ps o,out float4 tpos : SV_Position)
        {
            tpos = mul(MS2SS,float4(i.pos,1));
            o.color = i.color;
            o.uv0 = i.uv0;
        }
    };
    ps hlsl5
    {
        SamplerState sam0 : register(s0);
        Texture2D tex0 : register(t0);
  
        void main(in vs2ps i,out float4 r : SV_Target)
        {
            float4 tex = tex0.Sample(sam0,i.uv0);
        
            float f = saturate((tex.r - Para.x) * Para.y);           
            r = i.color*f;      // premultiplied alpha!
        }
    };

    // glsl1
  
    vs glsl1
    {
        void main()
        {
            gl_Position = MS2SS*vec4(i_pos,1);
            o_color = i_color;
            o_uv0 = i_uv0;
        }
    };

    ps glsl1
    {
        uniform sampler2D sam0;
        void main()
        {
            vec4 tex = texture2D(sam0,i_uv0);

            float f = ((tex.r - Para.x) * Para.y);
//            f = max(f,0.0);
            f = min(f,1.0);

            gl_FragColor = i_color * f;
        }
    };
}



/****************************************************************************/

mtrl sDistanceFieldFontShader2
{
  permute Outline;
  permute Halo;
  permute Multisample;
  permute Lighting;

  struct ia2vs
  {
    float3 pos : POSITION;
    float4 color : COLOR;
    float2 uv0 : TEXCOORD0;
  };
  struct vs2ps
  {
    float4 color : COLOR;
    float2 uv0 : TEXCOORD0;
  };
	
  cbuffer cbvs : vs(0) : register(0)
  {
    row_major float4x4 MS2SS;
  };
  
  cbuffer cbps : ps(0) : register(0)
  {
    float4 Color;
    float4 ColorO;
    float4 ColorH;
    float4 Ambient;
    float3 Sub;
    float Gamma;
    float3 Mul;
    float du;
    float3 Light;
    float dv;
  };
  
  
  vs hlsl3
  {
    void main(in ia2vs i,out vs2ps o,out float4 tpos : POSITION)
    {
      tpos = mul(MS2SS,float4(i.pos,1));
      o.color = i.color;
      o.uv0 = i.uv0;
    }
  };
  ps hlsl3
  {
    SamplerState sam0 : register(s0);
  
    void main(in vs2ps i,out float4 r : COLOR)
    {
      pif(Multisample)
      {
        float4 t0 = tex2D(sam0,i.uv0+float2(0 ,0 ));
        float4 t1 = tex2D(sam0,i.uv0+float2(du,0 ));
        float4 t2 = tex2D(sam0,i.uv0+float2(0 ,dv));
        float4 t3 = tex2D(sam0,i.uv0+float2(du,dv));
        
        float4 tex = float4(t0.w,t1.w,t2.w,t3.w);
        float4 factorx = saturate((tex-Sub.x)*Mul.x);

        float4 r0 = Color;
        float4 r1 = Color;
        float4 r2 = Color;
        float4 r3 = Color;
        
        float4 alpha = factorx;
        pif(Outline)
        {
          float4 factory = saturate((tex-Sub.y)*Mul.y);
          r0 = r0*alpha.x + ColorO*(1-alpha.x);
          r1 = r1*alpha.y + ColorO*(1-alpha.y);
          r2 = r2*alpha.z + ColorO*(1-alpha.z);
          r3 = r3*alpha.w + ColorO*(1-alpha.w);
          alpha = factory;
        }
        pif(Halo)
        {
          float4 factorz = saturate((tex-Sub.z)*Mul.z);
          r0 = r0*alpha.x + ColorH*(1-alpha.x);
          r1 = r1*alpha.y + ColorH*(1-alpha.y);
          r2 = r2*alpha.z + ColorH*(1-alpha.z);
          r3 = r3*alpha.w + ColorH*(1-alpha.w);
          alpha = 1-pow(float4(1,1,1,1)-factorz,float4(Gamma,Gamma,Gamma,Gamma));
        }
        r0.w *= alpha.x;
        r1.w *= alpha.y;
        r2.w *= alpha.z;
        r3.w *= alpha.w;
        
        r = (r0+r1+r2+r3)*0.25;
      }
      pelse
      {
        float4 tex = tex2D(sam0,i.uv0);
        
        float3 factors = saturate((tex.www - Sub)*Mul);
        
        pif(Lighting)
        {
          float3 norm = normalize(tex.xyz-0.5);
          float intensity = saturate(dot(norm,Light));
          r = saturate(Ambient+Color*intensity);
        }
        pelse
        {
          r = Color;
        }
        
        float alpha = factors.x;
        pif(Outline)
        {
          r = r*alpha + ColorO*(1-alpha);
          alpha = factors.y;
        }
        pif(Halo)
        {
          r = r*alpha + ColorH*(1-alpha);
          alpha = 1-pow(1-factors.z,Gamma);
        }
        r.w *= alpha;
      }
    }
  };
}

/****************************************************************************/
/***                                                                      ***/
/***   (C) 2012 Dierk Ohlerich, all rights reserved                       ***/
/***                                                                      ***/
/****************************************************************************/

mtrl CubeShader 
{
  struct ia2vs
  {
    float3 pos : POSITION;
    float3 norm : NORMAL;
    float2 uv0 : TEXCOORD0;
  };

  struct vs2ps
  {
    float4 color : COLOR;
    float2 uv0 : TEXCOORD0;
  };

  cbuffer cbvs : vs(0) : register(0)
  {
    row_major float4x4 mat;
    float3 ldir;
  };

  // hlsl3
  
  vs hlsl3
  {
    void main(in ia2vs i,out vs2ps o,out float4 tpos : POSITION)
    {
      tpos = mul(mat,float4(i.pos,1));
      float l;
      l = max(dot(ldir,i.norm),0)+0.125;
      o.color = l;
      o.uv0 = i.uv0;
    }
  };

  ps hlsl3
  {
    SamplerState sam0 : register(s0);
    void main(in vs2ps i,out float4 r : COLOR)
    {
      r = i.color * tex2D(sam0,i.uv0);
    }
  };

  // hlsl5
  
  vs hlsl5
  {
    void main(in ia2vs i,out vs2ps o,out float4 tpos : SV_Position)
    {
      tpos = mul(mat,float4(i.pos,1));
      float l;
      l = max(dot(ldir,i.norm),0)+0.125;
      o.color = l;
      o.uv0 = i.uv0;
    }
  };

  ps hlsl5
  {
    SamplerState sam0 : register(s0);
    Texture2D tex0 : register(t0);
    void main(in vs2ps i,out float4 r : SV_Target)
    {
      r = i.color * tex0.Sample(sam0,i.uv0);
    }
  };

  // glsl1
  
  vs glsl1
  {
    void main()
    {
      gl_Position = mat*vec4(i_pos,1);
      float l;
      l = max(dot(ldir,i_norm),0)+0.125;
      o_color = vec4(l,l,l,l);
      o_uv0 = i_uv0;
    }
  };

  ps glsl1
  {
    uniform sampler2D sam0;
    void main()
    {
      gl_FragColor = i_color * texture2D(sam0,i_uv0);
    }
  };
} 

/****************************************************************************/
